package com.example.demo.service;

import com.example.demo.entity.MeetingUsage;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class WeLinkApiServiceImpl implements WeLinkApiService{

    @Value("${welink.api.url}")
    private String apiUrl;

    @Value("${welink.api.appId}")
    private String appId;

    @Value("${welink.api.appSecret}")
    private String appSecret;

    private final OkHttpClient client = new OkHttpClient();
    private final ObjectMapper objectMapper = new ObjectMapper();


    @Override
    public List<MeetingUsage> getMeetingUsage() throws IOException {
        // 构建请求URL
        HttpUrl httpUrl = HttpUrl.parse(apiUrl)
                .newBuilder()
                .addQueryParameter("appId", appId)
                .addQueryParameter("appSecret", appSecret)
                .build();

        // 构建请求
//        Request request = new Request.Builder()
//                .url(httpUrl)
//                .get()
//                .build();
        // 发送请求
//        try (Response response = client.newCall().execute()) {
//            if (response.isSuccessful() && response.body() != null) {
//                String responseData = response.body().string();
//                // 假设API返回的是JSON数组，将其转换为MeetingUsage列表
//                return objectMapper.readValue(responseData, objectMapper.getTypeFactory().constructCollectionType(List.class, MeetingUsage.class));
//            }
//        }
        return null;
    }
}
